package com.eminem.service.impl;

import com.eminem.dao.DeptDao;
import com.eminem.model.Dept;
import com.eminem.service.DeptService;

import java.util.*;

public class DeptServiceimpl implements DeptService {

    private DeptDao deptDao = new DeptDao();

    @Override
    public List<Dept> getDeptInfo() {
        return deptDao.findDeptAll();
    }

    @Override
    public List<Map<String, Object>> getDeptList() {
        //查出来的部门所有信息
        List<Map<String, Object>> list = deptDao.findDeptList();
        //定义一个部门列表集合存储数据返回出去
        List<Map<String, Object>> deptList = new ArrayList<>();
        //定义一个set集合，不能有重复的数据
        Set<String> ids= new HashSet<>();

        Map<String , Object> deptMap;
        for (Map<String, Object> map : list) {
            //拿到部门id
            String deptId = map.get("id").toString();
            //判断没有才存进去
            if (!ids.contains(deptId)){
                deptMap = new HashMap<>();
                deptMap.put("deptId",deptId);
                deptMap.put("deptName",map.get("name").toString());
                if (null == map.get("user_id")){
                    deptMap.put("deptCount",0);
                }else {
                    deptMap.put("deptCount",1);
                }
                deptList.add(deptMap);
            }else {
                for (Map<String, Object> dept : deptList) {
                    if (deptId.equals(dept.get("deptId").toString())){
                        int count = Integer.parseInt(dept.get("deptCount").toString());;
                        dept.put("deptCount",++count);
                        break;
                    }
                }
            }
            ids.add(deptId);
        }
        //这是当前部门下单所有的员工存储
        List<Map<String,Object>> userList;
        Map<String,Object> userMap;
        for (Map<String, Object> dept : deptList) {
            String deptId = dept.get("deptId").toString();
            userList = new ArrayList<>();
            for (Map<String, Object> map : list) {
                String deptId1 = map.get("id").toString();
                if (deptId.equals(deptId1)){
                    userMap = new HashMap<>();
                    userMap.put("userId",map.get("id"));
                    userMap.put("realName",map.get("real_name"));
                    userList.add(userMap);
                }
            }
            dept.put("userList",userList);
        }

        return deptList;
    }

    @Override
    public List<Map<String, Object>> getDeptSystemList(String userId, int currentPage, String name) {
        return deptDao.findSystemList(userId,currentPage,name);
    }

    @Override
    public List<Map<String, Object>> getDeptName() {
        return deptDao.findDeptName();
    }

    @Override
    public long getSystemCount(String name) {
        return deptDao.getCount(name);
    }

    @Override
    public Map<String, Object> getSystemContent(String id) {
        return deptDao.getSystemContent(id);
    }


}
